package cn.elwy.osgi.config;

import java.io.File;

import cn.elwy.common.config.Config;
import cn.elwy.common.util.AssertUtil;
import cn.elwy.common.util.io.PropertyUtil;
import cn.elwy.osgi.config.model.WindowConfig;
import cn.elwy.osgi.config.util.ResourceUtil;

/**
 * 读取系统默认配置信息
 * @author huangsq
 * @version 2.0, 2013-10-01
 * @since 1.0, 2013-02-19
 */
public class AppConfig extends Config implements Constant {

	private static AppConfig appConfig;

	private AppConfig() {
	}

	public void load() {
		load(APP_CONFIG_FILE);
		init();
	}

	public static synchronized AppConfig getInstance() {
		if (appConfig == null) {
			appConfig = new AppConfig();
		}
		return appConfig;
	}

	public static void dispose() {
		appConfig = null;
	}

	public void init() {
	}

	public void initWindowConfig(WindowConfig windowConfig) {
		windowConfig.setTitle(propertyUtil.get("title", ""));
		windowConfig.setShellSstyle(propertyUtil.get("shell.style", "SHELL_TRIM"));
		windowConfig.setAutoCenter(propertyUtil.getBoolean("auto.enter", true));
		windowConfig.setMaximized(propertyUtil.getBoolean("maximized", true));
		windowConfig.setInitialWidth(propertyUtil.getInt("initial.width", 800));
		windowConfig.setInitialHeight(propertyUtil.getInt("initial.height", 600));
		windowConfig.setMinimumWidth(propertyUtil.getInt("minimum.width", 800));
		windowConfig.setMinimumHeight(propertyUtil.getInt("minimum.height", 600));
		windowConfig.setShowCoolBar(propertyUtil.getBoolean("show.cool.bar", true));
		windowConfig.setShowMenuBar(propertyUtil.getBoolean("show.menu.bar", true));
		windowConfig.setShowStatusLine(propertyUtil.getBoolean("show.status.line", true));
		windowConfig.setShowFastViewBars(propertyUtil.getBoolean("show.fast.view.bars", true));
		windowConfig.setShowProgressIndicator(propertyUtil.getBoolean("show.progress.indicator", true));
		windowConfig.setShowPerspectiveBar(propertyUtil.getBoolean("show.perspective.bar", true));
	}

	public File getJavaHome() {
		String javaHomePath = propertyUtil.get("java.home");
		String envJavaHome = propertyUtil.get("env.java.home");
		String javaHome = System.getenv(envJavaHome);
		if (AssertUtil.isNotEmpty(javaHomePath)) {
			return new File(javaHomePath);
		} else if (javaHome != null) {
			return new File(javaHome);
		} else {
			String javaDir = propertyUtil.get("java.dir", "jre");
			return new File(ResourceUtil.getInstallPath(), javaDir);
		}
	}

	public File getMavenHome() {
		String mavenHomePath = propertyUtil.get("maven.home");
		String envMavenHome = propertyUtil.get("env.maven.home");
		String mavenHome = System.getenv(envMavenHome);
		if (AssertUtil.isNotEmpty(mavenHomePath)) {
			return new File(mavenHomePath);
		} else if (mavenHome != null) {
			return new File(mavenHome);
		} else {
			String mavenDir = propertyUtil.get("maven.dir", "maven");
			return new File(ResourceUtil.getInstallPath(), mavenDir);
		}
	}

	public String getThemeName() {
		return propertyUtil.get("theme.name", "DEFAULT").toUpperCase();
	}

	public String getThemeDir() {
		return propertyUtil.get("theme.dir", "theme");
	}

	public Boolean isGetDaoByName() {
		return propertyUtil.getBoolean("get.dao.by.name", true);
	}

	public Boolean isGetServiceByName() {
		return propertyUtil.getBoolean("get.service.by.name", false);
	}

	public String getDaoPrefix() {
		return propertyUtil.get("dao.prefix", "");
	}

	public String getServicePrefix() {
		return propertyUtil.get("service.prefix", "");
	}

	public String getAuditPrefix() {
		String auditPrefix = propertyUtil.get("audit.prefix", "");
		if ("logger".equalsIgnoreCase(auditPrefix)) {
			auditPrefix = "";
		}
		return auditPrefix;
	}

	public boolean getUsedLastNewProject() {
		return propertyUtil.getBoolean("used.last.new.project", true);
	}

	public String getSaveStrategy() {
		return propertyUtil.get("save.strategy", "ALL").toUpperCase();
	}

	public String getDialogConfigDir() {
		return propertyUtil.get("dialog.config.dir", "dialogConfig");
	}

	public String getTableConfigDir() {
		return propertyUtil.get("table.config.dir", "tableConfig");
	}

	public String getSubjectType() {
		return propertyUtil.get("subject.type", "USER").toUpperCase();
	}

	public PropertyUtil getPropertyUtil() {
		return propertyUtil;
	}

}
